package com.peixunp.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.peixunp.bean.Organizations;
import com.peixunp.bean.Remark;
import com.peixunp.bean.RemarkTemplate;
import com.peixunp.dao.OrganizationsDao;
import com.peixunp.dao.RemarkDao;
import com.peixunp.dao.RemarkTemplateDao;
import com.peixunp.service.RemarkService;
import com.peixunp.util.Constants;

import finaler.util.DateUtil;
import finaler.util.Page;
import finaler.util.PageList;

public class RemarkServiceImpl implements RemarkService {

	@Autowired
	private RemarkDao remarkDao;
	
	@Autowired
	private OrganizationsDao organizationsDao;
	
	@Autowired
	private RemarkTemplateDao remarkTemplateDao;
	
	@Override
	public PageList getRemarkList(Page page) throws Exception {
		return remarkDao.getRemarkList(page);
	}

	@Override
	public boolean insert(Remark remark) throws Exception {
		return remarkDao.insert(remark);
	}

	@Override
	public int getAvgFen(int orgId) throws Exception {
		return remarkDao.getAvgFen(orgId);
	}

	@Override
	public PageList getGoodOrBadList(Page page) throws Exception {
		return remarkDao.getGoodOrBadList(page);
	}
	//自动每30秒写一个评论
	@Override
	public void autoCreateRemark() throws Exception {
		Organizations organizations = organizationsDao.getRandomRecord();//随机获得一个学校
		RemarkTemplate remarkTemplate = remarkTemplateDao.getRandomRecord();//随机获得一条评论模板
		Remark newRemark = new Remark();
		newRemark.setRmcontent(remarkTemplate.getRmcontent());
		newRemark.setAutoflag(1);
		newRemark.setCoin(remarkTemplate.getCoin());
		int ipStart = new Random().nextInt(255);
		ipStart = ipStart<60?(ipStart+150):ipStart;
		String ip = ipStart + "."+new Random().nextInt(255)+ "."+new Random().nextInt(255)+"."+new Random().nextInt(255);
		newRemark.setCreateuser(ip);
		newRemark.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		newRemark.setOrgid(organizations.getId());
		remarkDao.insert(newRemark);
	}

	//自动每5分钟生成首页的最新50个评论
	@Override
	public void autoCreate50newRemark(String index50remark_file_real_path) throws Exception {
		OutputStreamWriter out = null;
		String rn = System.getProperty("line.separator");//System.getProperty("line.separator")的意思是 "\r\n"
		StringBuffer new50remarkBuffer = new StringBuffer();
		new50remarkBuffer.append("<%@ page contentType=\"text/html; charset=UTF-8\" %>" + rn );
		List<Map<String, Object>> new50RemarkMapList = remarkDao.getNew50Remark();
		for (Map<String, Object> new50Remark : new50RemarkMapList) {
			new50remarkBuffer.append("<img src=\""+Constants.URL+"/images/arrow.gif\" /><font color=red>["+new50Remark.get("orgname")+"]</font>");
			new50remarkBuffer.append("<a target=\"_blank\" href=\""+Constants.URL+"/school/"+new50Remark.get("city")+"_"+new50Remark.get("orgid")+".html\">");
			new50remarkBuffer.append(new50Remark.get("rmcontent"));
			new50remarkBuffer.append("</a><br/>"+ rn);
		}
		try {
			//File.separator是跨平台的路径符
			out = new OutputStreamWriter(new FileOutputStream(index50remark_file_real_path + File.separator + "50newremark.jsp"),"UTF-8" );
			out.write( new50remarkBuffer.toString() );
		} catch (Exception e) {
			if(Constants.ISDEBUG){
				e.printStackTrace();
			}
		}finally{
			 if (out != null) {
	                try {
	                	out.flush();
	                	out.close();
	                } catch (IOException ioe) {
	        			if(Constants.ISDEBUG){
	        				ioe.printStackTrace();
	        			}
	                }
	            }
		}
	}
	
	//首页5个最高分
	@Override
	public void autoCreate5HighCoin(String file_real_path) throws Exception {
		OutputStreamWriter out = null;
		String rn = System.getProperty("line.separator");//System.getProperty("line.separator")的意思是 "\r\n"
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<%@ page contentType=\"text/html; charset=UTF-8\" %>" + rn );
		List<Map<String, Object>> highCoinList = remarkDao.get5HighCoin();
		for (Map<String, Object> highCoin : highCoinList) {
			stringBuffer.append("<div class=\"right_articles\">");
			stringBuffer.append("<p><img src=\""+Constants.URL+"/images/good.png\" alt=\"Image\"class=\"image\" />");
			stringBuffer.append("<b><a href=\""+Constants.URL+"/school/"+highCoin.get("city")+"_"+highCoin.get("id")+".html\">"+highCoin.get("orgname")+"</a></b><br/>");
			stringBuffer.append("联系电话:"+highCoin.get("orgtel")+"<br/>");
			stringBuffer.append("浏览数:"+highCoin.get("hit")+"&nbsp;&nbsp;&nbsp;总评分:"+highCoin.get("avgcoin"));
			stringBuffer.append("</p>");
			stringBuffer.append("</div>");
		}
		try {
			//File.separator是跨平台的路径符
			out = new OutputStreamWriter(new FileOutputStream(file_real_path + File.separator + "5highcoin.jsp"),"UTF-8" );
			out.write( stringBuffer.toString() );
		} catch (Exception e) {
			if(Constants.ISDEBUG){
				e.printStackTrace();
			}
		}finally{
			if (out != null) {
                try {
                	out.flush();
                	out.close();
                } catch (IOException ioe) {
        			if(Constants.ISDEBUG){
        				ioe.printStackTrace();
        			}
                }
            }
		}
	}

	//首页5个最多评
	@Override
	public void autoCreate5MostRemark(String file_real_path) throws Exception {
		OutputStreamWriter out = null;
		String rn = System.getProperty("line.separator");//System.getProperty("line.separator")的意思是 "\r\n"
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<%@ page contentType=\"text/html; charset=UTF-8\" %>" + rn );
		List<Map<String, Object>> mostRemarkList = remarkDao.get5MostRemark();
		for (Map<String, Object> mostRemark : mostRemarkList) {
			stringBuffer.append("<div class=\"right_articles\">");
			stringBuffer.append("<p><img src=\""+Constants.URL+"/images/high.png\" alt=\"Image\"class=\"image\" />");
			stringBuffer.append("<b><a href=\""+Constants.URL+"/school/"+mostRemark.get("city")+"_"+mostRemark.get("id")+".html\">"+mostRemark.get("orgname")+"</a></b><br/>");
			stringBuffer.append("联系电话:"+mostRemark.get("orgtel")+"<br/>");
			stringBuffer.append("浏览数:"+mostRemark.get("hit")+"&nbsp;&nbsp;&nbsp;评论数:"+mostRemark.get("rmcount"));
			stringBuffer.append("</p>");
			stringBuffer.append("</div>");
		}
		try {
			//File.separator是跨平台的路径符
			out = new OutputStreamWriter(new FileOutputStream(file_real_path + File.separator + "5mostremark.jsp"),"UTF-8" );
			out.write( stringBuffer.toString() );
		} catch (Exception e) {
			if(Constants.ISDEBUG){
				e.printStackTrace();
			}
		}finally{
			 if (out != null) {
	                try {
	                	out.flush();
	                	out.close();
	                } catch (IOException ioe) {
	        			if(Constants.ISDEBUG){
	        				ioe.printStackTrace();
	        			}
	                }
	            }
		}
		
	}

}
